// 导入数据库
const db = require('../db/index')

/**
 * message_title 消息主题
 * message_category 消息类别
 * message_publish_department 消息发布部门
 * message_publish_person 消息发布者
 * message_receiver 消息接收者
 * message_click_number 消息查看数量
 * message_content 消息内容
 * message_create_time 消息发布时间
 * message_update_time 消息更新时间
 * message_level 消息等级
 * message_status 默认为0 ，1在回收站
 * message_delete_time 消息删除时间
 */

// 消息发布 data =>
exports.publishMessage = (req, res) => {
    const { message_title, message_category, message_publish_department, message_publish_person,
        message_receiver, message_content, message_level
    } = req.body
    const message_click_number = 0
    const message_create_time = new Date()
    const message_status = 0
    const sql1 = "insert into message set ?"
    db.query(sql1, {
        message_title, message_category, message_publish_department: message_publish_department || '系统', message_publish_person, message_receiver: message_receiver || '全体部门',
        message_content, message_level: message_level || '系统', message_click_number, message_create_time, message_status
    }, (err, result) => {
        if (err) return res.cc(err)
        res.send({ status: 0, message: '消息发布成功', department: message_receiver || '全体部门', id: result.insertId })
    })
}

// 编辑消息 data =>
exports.editMessage = (req, res) => {
    const { id, message_title, message_receiver, message_content, message_level } = req.body
    // 通过id返回消息之前的部门
    const returnOldReceiver = id => {
        return new Promise(resolve => {
            const sql = 'select message_receiver from message where id = ?'
            db.query(sql, id, (err, result) => {
                resolve(result[0].message_receiver)
            })
        })
    }
    // 执行函数
    async function change() {
        const oldReceiver = await returnOldReceiver(id)
        const message_update_time = new Date()
        const sql2 = "update message set ? where id = ?"
        db.query(sql2, [{
            message_title, message_receiver, message_content, message_level, message_update_time
        }, id], (err, result) => {
            if (err) return res.cc(err)
            res.send({ status: 0, message: '消息编辑成功', oldReceiver, message_receiver })
        })
    }
    change()
}

// 通过部门筛选消息 message_publish_department =>
exports.searchMessageByDepartment = (req, res) => {
    const { message_publish_department } = req.body
    const sql1 = "select * from message where message_publish_department = ? and message_status = 0"
    db.query(sql1, message_publish_department, (err, result) => {
        if (err) return res.cc(err)
        res.send(result)
    })
}

// 通过等级筛选消息 message_level =>
exports.searchMessageByLevel = (req, res) => {
    const { message_level } = req.body
    const sql1 = "select * from message where message_level = ? and message_status = 0"
    db.query(sql1, message_level, (err, result) => {
        if (err) return res.cc(err)
        res.send(result)
    })
}

// 更新点击率 id =>
exports.updateClickNumber = (req, res) => {
    const { id } = req.body
    db.query("select * from message where id = ?", id, (err, result) => {
        if (err) return res.cc(err)
        const sql1 = "update message set message_click_number = ? where id = ?"
        db.query(sql1, [result[0].message_click_number + 1, id], (err, result) => {
            if (err) return res.cc(err)
            res.send({ status: 0, message: '更新点击率成功' })
        })
    })
}

// 初次删除消息 id =>
exports.deleteMessage = (req, res) => {
    const { id } = req.body
    const sql1 = "update message set ? where id = ?"
    db.query(sql1, [{ message_status: 1, message_delete_time: new Date() }, id], (err, result) => {
        if (err) return res.cc(err)
        res.send({ status: 0, message: '删除消息成功' })
    })
}

// 彻底删除消息 id =>
exports.realDeleteMessage = (req, res) => {
    const { id } = req.body
    const sql1 = "delete from message where id = ?"
    db.query(sql1, id, (err, result) => {
        if (err) return res.cc(err)
        res.send({ status: 0, message: '彻底删除消息成功' })
    })
}

// 恢复消息 id =>
exports.recoverMessage = (req, res) => {
    const { id } = req.body
    const sql1 = "update message set ? where id = ?"
    db.query(sql1, [{ message_status: 0, message_delete_time: null, message_update_time: new Date() }, id], (err, result) => {
        if (err) return res.cc(err)
        res.send({ status: 0, message: '恢复消息成功' })
    })
}

// 根据类别筛选回收列表消息 message_category =>
exports.searchRecycleByCategory = (req, res) => {
    const { message_category } = req.body
    const sql1 = "select * from message where message_category = ? and message_status = 1"
    db.query(sql1, message_category, (err, result) => {
        if (err) return res.cc(err)
        res.send(result)
    })
}

// 分页实现=============================
// 获取消息列表总数 message_category =>
exports.getMessageNumber = (req, res) => {
    const { message_category } = req.body
    const sql1 = "select * from message where message_status = 0 and message_category = ?"
    db.query(sql1, message_category, (err, result) => {
        if (err) return res.cc(err)
        res.send({
            length: result.length
        })
    })
}
// 消息列表监听换页 页码pager,message_category =>
exports.returnMessageData = (req, res) => {
    // 传来的页码
    const { pager, message_category } = req.body
    const number = (pager - 1) * 10
    // 一页10个数据
    // limit 10 offset ${number} 表示从第number个起取10个
    const sql1 = `select * from message where message_status = 0 and message_category = ? order by message_create_time limit 10 offset ${number}`
    db.query(sql1, message_category, (err, result) => {
        if (err) {
            return res.cc(err)
        }
        res.send(result)
    })
}
// 获取回收站列表总数 无 =>
exports.getRecycleNumber = (req, res) => {
    const sql1 = "select * from message where message_status = 1"
    db.query(sql1, (err, result) => {
        if (err) return res.cc(err)
        res.send({
            length: result.length
        })
    })
}
// 回收列表监听换页 页码pager =>
exports.returnRecycleData = (req, res) => {
    // 传来的页码
    const { pager } = req.body
    const number = (pager - 1) * 10
    // 一页10个数据
    // limit 10 offset ${number} 表示从第number个起取10个
    const sql1 = `select * from message where message_status = 1 order by message_create_time limit 10 offset ${number}`
    db.query(sql1, (err, result) => {
        if (err) {
            return res.cc(err)
        }
        res.send(result)
    })
}

// 首页获取公司公告列表 无 =>
exports.getNoticeList = (req, res) => {
    const sql1 = "select * from message where message_category = '公司公告' and message_status = 0 order by message_create_time DESC limit 5"
    db.query(sql1, (err, result) => {
        if (err) return res.cc(err)
        res.send(result)
    })
}

// 首页获取系统消息列表 无 =>
exports.getSystemList = (req, res) => {
    const sql1 = "select * from message where message_category = '系统消息' and message_status = 0 order by message_create_time DESC limit 5"
    db.query(sql1, (err, result) => {
        if (err) return res.cc(err)
        res.send(result)
    })
}